﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

/// <summary>
/// Summary description for Login
/// </summary>
public class ProLogin
{
    public ProLogin()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    //null: Don't exist 
    //UserInfo Object : exist
    public UserInfo CheckLogin(UserInfo userInfo)
    {
        //Connect
        ConnectDB db = new ConnectDB();

        DataSet ds = new DataSet();
        ds = db.getUser(userInfo);

        if (ds.Tables[0].Rows.Count == 0)
            return null;
        else
        {
            userInfo = setUser(ds, userInfo, db);
        }
        return userInfo;
    }

    //ds is user data;
    public UserInfo setUser(DataSet ds, UserInfo us, ConnectDB db)
    {
        us.StrFullName = ds.Tables[0].Columns[2].ToString();//full name
        us.StrAddress = ds.Tables[0].Columns["address"].ToString();

        DataSet dsDep = new DataSet();
        dsDep = db.getDepartment(Int32.Parse(ds.Tables[0].Rows[0]["department"].ToString()));
        us.DepDepartment.Id = Int32.Parse(dsDep.Tables[0].Rows[0]["id"].ToString());
        us.DepDepartment.StrDepartment = dsDep.Tables[0].Rows[0]["department"].ToString();

        us.DateBirthday = DateTime.Parse(ds.Tables[0].Rows[0]["birthday"].ToString());
        us.StrPhone = ds.Tables[0].Rows[0]["phone"].ToString();

        DataSet dsPos = new DataSet();
        dsPos = db.getPosition(Int32.Parse(ds.Tables[0].Rows[0]["position"].ToString()));
        us.PosPosition.Id = Int32.Parse(dsPos.Tables[0].Rows[0]["id"].ToString());
        us.PosPosition.StrPosition = dsPos.Tables[0].Rows[0]["position_name"].ToString();

        us.StrEmail = ds.Tables[0].Rows[0]["email"].ToString();

        DataSet dsStatus = new DataSet();
        dsStatus = db.getStatusUser(Int32.Parse(ds.Tables[0].Rows[0]["status"].ToString()));
        us.StaStatus.Id = Int32.Parse(dsStatus.Tables[0].Rows[0]["id"].ToString());
        us.StaStatus.StrStatus = dsStatus.Tables[0].Rows[0]["status"].ToString();

        //Read Signature image from database

        return us;
    }



}
